home *** CD-ROM | disk | FTP | other *** search
/ Hyper Stacks 1994 May / Hyper Stacks (Pacific HiTech)(1994)[Mac].iso / Utilities / Pictoids 1.2 Update / Pictoid Demo ƒ / WDEF Demo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-04  |  5.2 KB  |  239 lines  |  [TEXT/KAHL]

  1. /* trivial demo of Pictoid WDEF, Nigel Perry 90, 91, 92 */
  2. /* Comments? This program is trivial… */
  3.  
  4. #include "Pictoid.h"
  5.  
  6. #ifndef NIL
  7. #define NIL ((void *)0)
  8. #endif
  9. #define FRONTWIN ((WindowPtr)-1)
  10.  
  11. #define BIG_DLOG 128
  12. #define BIG_DISMISS 1
  13.  
  14. /* used to check for NewCWindow */
  15.  
  16. #include <Traps.h>
  17.  
  18. #pragma parameter __A0 GetOSTrapAddr(__D0)
  19. static pascal long GetOSTrapAddr(int trap) = 0xA346;
  20.  
  21. #pragma parameter __A0 GetToolTrapAddr(__D0)
  22. static pascal long GetToolTrapAddr(int trap) = 0xA746;
  23.  
  24. static Boolean TrapAvailable(int trap)
  25. {    register long unimp = GetToolTrapAddr(_Unimplemented);
  26.     register long addr;
  27.  
  28.     if(trap & 0x0800) /* toolbox */
  29.     {    trap &= 0x7FF;
  30.         if( (trap >= 0x0400)
  31.             || (((addr = GetToolTrapAddr(_InitGraf))
  32.                     == GetToolTrapAddr(_InitGraf | 0x0200))
  33.                  && (trap >= 0x0200)
  34.                )
  35.           ) trap = _Unimplemented;
  36.         addr = GetToolTrapAddr(trap);
  37.     }
  38.     else
  39.         addr = GetOSTrapAddr(trap);
  40.     return addr != unimp;
  41. }
  42.  
  43. /* used to check if window was framed */
  44. static framed(w, wl, base)
  45. WindowPtr w, *wl;
  46. int base;
  47. {    int i;
  48.  
  49.     i = 8;
  50.     while(i--)
  51.     {    if(w == *wl++)
  52.             return(base & PW_AddFrame);
  53.         base++;
  54.     }
  55.     
  56.     return(0);
  57. }
  58.  
  59. /* used to check if window is a "picture" window */
  60. static picwin(w, wl, base)
  61. WindowPtr w, *wl;
  62. int base;
  63. {    int i;
  64.  
  65.     i = 8;
  66.     while(i--)
  67.     {    if(w == *wl++)
  68.             return(base >= 12);
  69.         base++;
  70.     }
  71.     
  72.     return(0);
  73. }
  74.  
  75. eventLoop(WindowPtr *w, int base)
  76. {    int stop;
  77.     Rect r1;
  78.     WindowPtr wind;
  79.     EventRecord event;
  80.     Rect size;
  81.     
  82.     stop = 0;
  83.     size.left = 4; size.right = 32767;
  84.     size.top = 4; size.bottom = 32767;
  85.     r1.top = 0; r1.left = 0; r1.bottom = 32767; r1.right = 32767;
  86.     
  87.     while(!stop)
  88.     {    if( GetNextEvent(everyEvent, &event) )
  89.             switch( event.what )
  90.             {    case updateEvt:
  91.                     BeginUpdate((WindowPtr)event.message);
  92.                     EndUpdate((WindowPtr)event.message);
  93.                     break;
  94.                 case keyDown:
  95.                     stop = 1;
  96.                     break;
  97.                 case mouseDown:
  98.                     switch( FindWindow(event.where, &wind) )
  99.                     {    case inContent:
  100.                             if( picwin(wind, w, base) )
  101.                             {    if( wind != FrontWindow() )
  102.                                     SelectWindow(wind);
  103.                                 else
  104.                                 {    SetPort(wind);
  105.                                     FillRect(&r1, gray);
  106.                                     while( StillDown() );
  107.                                     EraseRect(&r1);
  108.                                 }
  109.                             }
  110.                             else if( framed(wind, w, base) )
  111.                             {    if( wind != FrontWindow() )
  112.                                     SelectWindow(wind);
  113.                                 else
  114.                                     DragWindow(wind, event.where, &screenBits.bounds);
  115.                             }
  116.                             /* fall thru */
  117.                         case inDrag:
  118.                             if( event.modifiers & 0x0800 ) /* option key */
  119.                             {    /* in asm as GrowWindow returns a long and 
  120.                                     SizeWindow wants two ints… */
  121.                                 asm
  122.                                 {    subq.l    #4,a7
  123.                                     move.l    wind,-(a7)
  124.                                     move.l    event.where,-(a7)
  125.                                     pea        size
  126.                                     GrowWindow
  127.                                     move.l    (a7)+,d0
  128.                                     beq.s        @nogrow
  129.                                     move.l    wind,-(a7)
  130.                                     move.l    d0,-(a7)
  131.                                     move.w    #0,-(a7)
  132.                                     SizeWindow
  133.                                     ; CopyRgn(strucRgn, updateRgn) -> invalidate
  134.                                     ; whole window area
  135.                                     move.l    wind,a0
  136.                                     move.l    OFFSET(WindowRecord,strucRgn)(a0),-(a7)
  137.                                     move.l    OFFSET(WindowRecord,updateRgn)(a0),-(a7)
  138.                                     CopyRgn
  139.                                 nogrow:
  140.                                 }
  141.                             }
  142.                             else
  143.                                 DragWindow(wind, event.where, &screenBits.bounds);
  144.                             break;
  145.                         default:
  146.                             break;
  147.                     }
  148.                     break;
  149.                 default:
  150.                     break;
  151.             }
  152.     }
  153. }
  154.  
  155. setupWindows(int variant, WindowPtr *wl, PicHandle p1, PicHandle p2,
  156.                     PicHandle p3, PicHandle p4, WDEFGlobals *gp)
  157. {    EventRecord event;
  158.     Rect r1, r2;
  159.     int h, v;
  160.     Boolean isColour = TrapAvailable(_GetNewCWindow);
  161.  
  162.     r1.top = 16; r1.left = 16; r1.bottom = 106; r1.right = 86;
  163.  
  164.     gp->pic = p1;
  165.     gp->frame = p2;
  166.     gp->flags.clonePic = gp->flags.cloneFrame = false;
  167.     gp->flags.sharePic = gp->flags.shareFrame = true;
  168.     
  169.     /* do eight variations starting from variant */
  170.     for(v = 0; v <= 240; v += 80)
  171.         for(h = 0; h <= 256; h += 256)
  172.         {    r2 = r1;
  173.             OffsetRect(&r2, h, v);
  174.             if(variant == 12) { gp->pic = p3; gp->frame = p4; }
  175.             if(isColour)
  176.                 *wl++ = (WindowPtr)NewCWindow(NIL, &r2, "\p", true, PictWDEF | variant, FRONTWIN, false, 0);
  177.             else
  178.                 *wl++ = (WindowPtr)NewWindow(NIL, &r2, "\p", true, PictWDEF | variant, FRONTWIN, false, 0);
  179.             /* NewWindow() will not draw the window's picture,
  180.                 calling CheckUpdate() will fix this - all subsequent
  181.                 redraws will be handled automatically by QD
  182.              */
  183.             CheckUpdate(&event);
  184.             variant++;
  185.         }
  186. }
  187.  
  188. main()
  189. {    int j, stop;
  190.     WindowPtr w[16], wind;
  191.     PicHandle p1, p2, p3, p4;
  192.     EventRecord event;
  193.     WDEFGlobals *gp;
  194.     Rect size;
  195.     
  196.     InitGraf(&thePort);
  197.     InitFonts();
  198.     FlushEvents(everyEvent,0);
  199.     InitWindows();
  200.     InitMenus();
  201.     TEInit();
  202.     InitDialogs(NIL);
  203.     InitCursor();
  204.  
  205.     if(!doDialog()) return;
  206.  
  207.     if(GetWDEFGlobals(true, &gp)) return;
  208.     
  209.     /* some pictures to draw */
  210.     p1 = (PicHandle)GetResource('PICT', 103);
  211.     p2 = (PicHandle)GetResource('PICT', 104);
  212.     p3 = (PicHandle)GetResource('PICT', 101);
  213.     p4 = (PicHandle)GetResource('PICT', 102);
  214.     
  215.     /* do first eight variations */
  216.     setupWindows(0, w, p1, p2, p3, p4, gp);
  217.  
  218.     /* play with them */
  219.     eventLoop(w, 0);
  220.     
  221.     /* clean up */    
  222.     for(j = 0; j < 8; j++) DisposeWindow(w[j]);
  223.  
  224.     /* now next eight */
  225.     setupWindows(8, w, p1, p2, p3, p4, gp);
  226.  
  227.     /* play with them */
  228.     eventLoop(w, 8);
  229.     
  230.     /* clean up */    
  231.     for(j = 0; j < 8; j++) DisposeWindow(w[j]);
  232.  
  233.     ReleaseResource(p1);
  234.     ReleaseResource(p2);
  235.     ReleaseResource(p3);
  236.     ReleaseResource(p4);
  237.     
  238. }
  239.